package com.asap.demo.model;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class Alarm implements Serializable {

	private static final long serialVersionUID = -5942870400092528058L;
	/**
	 * state 是否告警  1是 2否
	 * focus 重点关注，这关注什么？1是 2否
	 */
	private String state;
	private String focus;
	private String alarmType;
	private String alarmPhase;
	private String alarmGrade;
	private String alarmContext;
	private String alarmSubType;
	private String alarmThreeType;
	//notics 通知方式邮件mail、短信message
	private String[] notice;

	public String getAlarmSubType() {
		return alarmSubType;
	}

	public void setAlarmSubType(String alarmSubType) {
		this.alarmSubType = alarmSubType;
	}

	public String getAlarmThreeType() {
		return alarmThreeType;
	}

	public void setAlarmThreeType(String alarmThreeType) {
		this.alarmThreeType = alarmThreeType;
	}

	private List<String> contexts = new ArrayList<String>();
	private List<String> fields = new ArrayList<String>();

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getFocus() {
		return focus;
	}

	public void setFocus(String focus) {
		this.focus = focus;
	}

	public String getAlarmType() {
		return alarmType;
	}

	public void setAlarmType(String alarmType) {
		this.alarmType = alarmType;
	}

	public String getAlarmPhase() {
		return alarmPhase;
	}

	public void setAlarmPhase(String alarmPhase) {
		this.alarmPhase = alarmPhase;
	}

	public String getAlarmGrade() {
		return alarmGrade;
	}

	public void setAlarmGrade(String alarmGrade) {
		this.alarmGrade = alarmGrade;
	}

	public String getAlarmContext() {
		return alarmContext;
	}

	public void setAlarmContext(String alarmContext) {
		this.alarmContext = alarmContext;
	}

	public String[] getNotice() {
		return notice;
	}

	public void setNotice(String[] notice) {
		this.notice = notice;

	}

	/*
	 * context配置中包含{}，提取需要替代的字段，在输出是替换
	 * */
	public void parseContext(Map<String, String> nameToAttr) {
		int endIndex;
		int beginIndex;
		String[] array = alarmContext.split("}");
		for (int i = 0; i < array.length; i++) {
			beginIndex = 0;
			endIndex = array[i].indexOf('{');
			if (endIndex >= 0) {
				contexts.add(array[i].substring(beginIndex, endIndex));
				String field = array[i].substring(endIndex + 1);
				fields.add(nameToAttr.get(field));
			} else {
				contexts.add(array[i]);
				fields.add("");
			}
		}

	}

	public String getAlarmContext(StandardEvent newEvent) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < contexts.size(); i++) {
			sb.append(contexts.get(i));
			if (newEvent.getField(fields.get(i)) != null)
				sb.append(newEvent.getField(fields.get(i)));
		}
		return sb.toString();
	}

	@Override
	public String toString() {
		return "Alarm [state=" + state + ", focus=" + focus + ", alarmType=" + alarmType + ", alarmPhase=" + alarmPhase
				+ ", alarmGrade=" + alarmGrade + ", alarmContext=" + alarmContext + ", notice="
				+ Arrays.toString(notice) + "]";
	}
}
